from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)

# 模拟的用户数据
users = {
    'teacher': [],
    'student': [],
    'parent': []
}


@app.route('/', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        if 'login' in request.form:
            user_type = request.form['user_type']
            username = request.form['username']
            password = request.form['password']

            for user in users[user_type]:
                if user['username'] == username and user['password'] == password:
                    return redirect(url_for(f'{user_type}_dashboard'))
            return "登录失败，用户名或密码错误！"
        elif 'register' in request.form:
            return redirect(url_for('register'))
        elif 'forgot_password' in request.form:
            return redirect(url_for('forgot_password'))

    return render_template('login.html')


@app.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'POST':
        user_type = request.form['user_type']
        username = request.form['username']
        password = request.form['password']

        for user_type_key in users:
            for user in users[user_type_key]:
                if user['username'] == username:
                    return "用户名已存在，请选择其他用户名。"

        users[user_type].append({'username': username, 'password': password})
        return redirect(url_for('register_success'))

    return render_template('register.html')


@app.route('/register_success')
def register_success():
    return render_template('register_success.html')


@app.route('/forgot_password', methods=['GET', 'POST'])
def forgot_password():
    if request.method == 'POST':
        username = request.form['username']
        new_password = request.form['new_password']

        for user_type in users:
            for user in users[user_type]:
                if user['username'] == username:
                    user['password'] = new_password
                    return redirect(url_for('password_reset_success'))

        return "用户名不存在。"

    return render_template('forgot_password.html')


@app.route('/password_reset_success')
def password_reset_success():
    return render_template('password_reset_success.html')


@app.route('/student_dashboard')
def student_dashboard():
    return render_template('student_dashboard.html')


@app.route('/teacher_dashboard')
def teacher_dashboard():
    return render_template('teacher_dashboard.html')


@app.route('/parent_dashboard')
def parent_dashboard():
    return render_template('parent_dashboard.html')


if __name__ == '__main__':
    app.run(debug=True)
